// pages/account/register_next/register_next.js
import {
  REGISTER,
} from '../../../request/api.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName: '',
    password: '',
    phoneToken: '',
    gender: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.phoneToken){
      this.data.phoneToken = options.phoneToken;
    }
  },

  showToast: function(message){
    wx.showToast({
      title: message,
      icon: 'none'
    })
  },

  bindIdInput: function (e) {
    this.data.userName = e.detail.value;
  },
  bindPasswordInput: function (e) {
    this.data.password = e.detail.value;
  },

  bindGenderChange: function(e){
    this.data.gender = e.detail.value;
  },

  register: function () {
    var username = this.data.userName.trim();
    var password = this.data.password.trim();
    var phoneToken = this.data.phoneToken;
    var gender = this.data.gender;
    if(!username){
      this.showToast('请输入用户名');
      return;
    } else if(!password){
      this.showToast('请输入密码');
      return;
    }
    this.showLoading('正在提交...');
    REGISTER({
      'username': username,
      'password': password,
      'phoneToken': phoneToken,
      'gender': gender
    }).then(res => {
      this.hideLoading();
      if (res.data.code == 1) {
        var pages = getCurrentPages();
        var prePage = pages[pages.length - 4];
        prePage.setData({
          reload: true,
        });
        wx.navigateBack({
          delta: 3,
        });
      } else {
        this.showTips(res.data.message);
      }
    }).catch(err => {
      this.hideLoading();
    })
  },

  showTips: function(msg){
    wx.showToast({
      title: msg,
      icon: 'none'
    });
  },
  
  showLoading: function(title){
    wx.showLoading({
      title: title,
      mask: true,
      success: (res) => {},
      fail: (res) => {},
      complete: (res) => {},
    });
  },
  hideLoading: function(){
    wx.hideLoading({
      success: (res) => {},
    })
  }
})